package util

import (
	"core/configtable"
	"math/rand"
	"sync"
)

type NameMgr struct {
	first  []uint32
	second []uint32
}

var (
	name    *NameMgr
	cfgOnce sync.Once
)

func NameMgrGetMe() *NameMgr {
	cfgOnce.Do(func() {
		name = &NameMgr{}
	})
	return name
}

func (name *NameMgr) Init() {
	tbl := configtable.NameConfigTableMgr_GetMe()
	tbl.Datas.Range(func(key, value interface{}) bool {
		val, _ := value.(*configtable.NameConfigTable)
		tmp := SplitStringToUint32Slice(val.Name, "|")
		if val.UnitId == 1 {
			name.first = make([]uint32, len(tmp))
			copy(name.first, tmp)
		} else if val.UnitId == 2 {
			name.second = make([]uint32, len(tmp))
			copy(name.second, tmp)
		}
		return true
	})
}

func (name *NameMgr) RandName() string {
	if len(name.second) <= 0 || len(name.first) <= 0 {
		return ""
	}
	first := configtable.NameTableMgr_GetMe().Get(name.first[rand.Int()%len(name.first)])
	second := configtable.NameTableMgr_GetMe().Get(name.second[rand.Int()%len(name.second)])
	if first != nil && second != nil {
		return first.Chinese + second.Chinese
	}
	return ""
}
